// pages/store/store.js
const app = getApp()
import http from '../../utils/http'
import util from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: 0,
    current: 0,
    listHeight: 500,
    minHeight: 200,
    maxHeight: 700,
    startY: 0,
    keyword: '',
    dataList: [],
    address: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const windowInfo = wx.getWindowInfo()
    const statusBarHeight = windowInfo.statusBarHeight
    const minHeight = windowInfo.windowHeight * 0.3
    const maxHeight = windowInfo.windowHeight * 0.6
    this.setData({
      statusBarHeight,
      minHeight,
      maxHeight,
      listHeight: minHeight,
    })
  },
  change(e) {
    const { index } = e.currentTarget.dataset
    if (this.data.current === index) return;
    this.setData({
      current: index
    })
  },
  // 触摸开始
  handleTouchStart(e) {
    this.setData({
      startY: e.touches[0].clientY
    })
  },
  // 触摸移动
  handleTouchMove(e) {
    const touchY = e.touches[0].clientY
    const deltaY = this.data.startY - touchY
    const newHeight = this.data.listHeight + deltaY
    this.setData({
      listHeight: Math.min(
        Math.max(newHeight, this.data.minHeight),
        this.data.maxHeight
      ),
      startY: touchY
    })
  },
  // 确认自提点
  confirm() {
    if (this.data.current === -1 || !this.data.dataList.length) {
      wx.showToast({
        title: '请选择自提点',
        icon: 'none'
      });
      return;
    }
    
    const selectedStore = this.data.dataList[this.data.current];
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.emit('acceptAddress', {
      id: selectedStore.id,
      name: selectedStore.name,
      address: selectedStore.address,
      business_hours: selectedStore.business_hours,
      tel: selectedStore.tel,
      image: selectedStore.image
    });
    wx.navigateBack();
  },
  // 获取自提门店列表
  getOutletList() {
    const { address, keyword } = this.data
    const data = {
      city: address.city,
      district: address.district,
      keyword: keyword,
    }
    http.post('points/stores', data, true).then(res => {
      if (res.code === 1) {
        this.setData({
          dataList: res.data,
        })
      }
    })
  },
  selectAddress() {
    util.skip('/service/selectAddress/selectAddress?city=' + this.data.address.city + '&name=' + this.data.address.name)
  },
  bindinput(e) {
    const val = e.detail.value
    this.setData({
      keyword: val
    })
  },
  search() {
    this.getOutletList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onShowAddress() {
    this.setData({
      address: app.globalData.address
    })
    this.getOutletList()
  }
})